package com.genius.human.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.workflow.page.PageResult;
import com.genius.workflow.page.SearchResult;
import com.genius.workflow.page.SqlLang;
import com.genius.workflow.util.*;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@Service
public class HumanService {


//    @Autowired
//    HumanMapper humanService;


    public JSONArray getAllUserInformation(JSONObject field){

        try {
            JSONArray user_information = DBTools.selectArr(field.getString("headField"), "user_information");

            return user_information;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
//        List<UserInformation> userInformations = humanService.selectList(new QueryWrapper<>());
//        UserInformation userInformation = new UserInformation();
//        List<UserInformation> userInformations1 = userInformation.selectAll();

    }

    public JSONObject getSearchUser(JSONObject field){
        JSONObject response = new JSONObject();
        SqlLang sql = null;
        int limit = field.getIntValue(PageResult.limit);
        int page = field.getIntValue(PageResult.page);
        String headField = field.getString(PageResult.headField);
        String selectSB = "SELECT * FROM user_information ${where}";
        if(headField == null){
            headField = "*";
        }
            JSONObject jsonObject = new JSONObject();
            SearchResult whereFieldBySearch = DBTools2.getWhereFieldBySearch(field);
            SearchResult whereFieldByHighSearch = DBTools2.getWhereFieldByHighSearch(field);
            JSONObject jsonObject1 = DBTools2.mergeAll(jsonObject, whereFieldBySearch, whereFieldByHighSearch);
        String asc = "desc";
        String ascField = UserJson.name;
        sql = DBTools2.getSelectPageSql(headField,selectSB,page,limit,ascField,asc,whereFieldBySearch.getSearchWhere(),whereFieldByHighSearch.getSearchWhere());

        try {
            String parameterArr [] = DBTools2.parsePermeterBySql(sql.getSourceSql(), jsonObject1);
            PageResult result = DBUtils.selectByCount(sql,parameterArr);
            JSONArray data = (JSONArray)result.getData();
            for(int i =0;i<data.size();i++){
                GeniusJsonUtil.convertResource(data.getJSONObject(i),UserJson.resource);
            }
            response.put(ResponseJson.count, result.getCount());
            response.put(ResponseJson.data, data);
            response.put("code",0);
            response.put(ResponseJson.status, ResponseJson.status_Success);

        } catch (ConnectionException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
        } catch (DBClosedException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
        } catch (SQLException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_Query_Data_Error);
        }
        return response;
//        List<UserInformation> userInformations = humanService.selectList(new QueryWrapper<>());
//        UserInformation userInformation = new UserInformation();
//        List<UserInformation> userInformations1 = userInformation.selectAll();

    }

    public JSONObject getUser(String id){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",id);
        try {
            JSONObject select = DBTools2.select("*", "user_information", "id", jsonObject);
            return select;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
//        List<UserInformation> userInformations = humanService.selectList(new QueryWrapper<>());
//        UserInformation userInformation = new UserInformation();
//        List<UserInformation> userInformations1 = userInformation.selectAll();

    }


    public JSONArray getGongzuo(String id){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("user_id",id);
        try {
            JSONArray select = DBTools2.selectArr("gz_start,gz_over,gz_bumen,gz_gangwei", "user_gzjl", "user_id", jsonObject);

            return select;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
//        List<UserInformation> userInformations = humanService.selectList(new QueryWrapper<>());
//        UserInformation userInformation = new UserInformation();
//        List<UserInformation> userInformations1 = userInformation.selectAll();

    }


    public void updateUser(JSONObject json){
//        List<java.lang.String> list = new ArrayList<>();
//        json.forEach((k,v)->{
//            list.add(k);
//        });
        try {
            DBTools2.update("id,name,sex,phone,entry_time,work_age,department,post,state,nationality,nation,politics,education,school,code,birth,registered,address,number,expire,remarks","id","user_information",json);
            String[] user_informations = DBTools.getUpdateAndWhereParms("id,name,sex,phone,entry_time,work_age,department,post,state,nationality,nation,politics,education,school,code,birth,registered,address,number,expire,remarks", json, "user_information");
            System.out.println("a======================================================");
            System.out.println(user_informations.length);
            for (String user_information : user_informations) {
                System.out.println(user_information);
            }
            System.out.println("a======================================================");

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public int insert(JSONObject json,JSONArray list){
//        List<java.lang.String> list = new ArrayList<>();
//        json.forEach((k,v)->{
//            list.add(k);
//        });
//        try {
//            DBTools2.update("id,name,sex,phone,entry_time,work_age,department,post,state,nationality,nation,politics,education,school,code,birth,registered,address,number,expire,remarks","id","user_information",json);
//            String[] user_informations = DBTools.getUpdateAndWhereParms("id,name,sex,phone,entry_time,work_age,department,post,state,nationality,nation,politics,education,school,code,birth,registered,address,number,expire,remarks", json, "user_information");
//            System.out.println("a======================================================");
//            System.out.println(user_informations.length);
//            for (String user_information : user_informations) {
//                System.out.println(user_information);
//            }
//            System.out.println("a======================================================");
//
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }

            try {
                DBTools2.insert("name,sex,phone,entry_time,work_age,department,post,state,nationality,nation,politics,education,school,code,birth,registered,address,number,expire,remarks,yicunzhao,gerenjianli,gongzuozhaopian", "user_information", json);
                if (list==null){
                    return 0;
                }
                for (int i=0;i<=list.size()-1;i++){
                    JSONObject jsonObject = list.getJSONObject(i);
                    Connection connect = DBUtils.getConnect();
                    ResultSet query = DBUtils.query(connect, "select max(id) from user_information");
                    query.next();
                    int anInt = query.getInt("max(id)");
//                    String selectFieldFromSqlStr = DBTools2.getSelectFieldFromSqlStr("select max(id) from user_information");
                    System.out.println("这是下表" + anInt);
                    jsonObject.put("user_id",anInt);
                    DBTools2.insert("user_id,gz_start,gz_over,gz_bumen,gz_gangwei", "user_gzjl", jsonObject);
                }

            } catch (Exception e) {
                System.out.println("有错误");
                return 1;
            }


        return 0;
    }

}
